package com.qian.ddj.web.controller;

import com.qian.api.ddj.model.*;
import com.qian.api.ddj.service.IBusinessGroupService;
import com.qian.api.ddj.service.IBusinessService;
import com.qian.api.ddj.service.IChannelService;
import com.qian.api.ddj.service.IWithdrawService;
import com.qian.api.ddj.util.SignUtils;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/ddj/withdraw")
@Api(tags = "提现接口")
public class WithdrawController {

    @Autowired
    private IChannelService channelService;
    @Autowired
    private IBusinessGroupService businessGroupService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IWithdrawService withdrawService;

    @GetMapping("/channelList")
    @ApiOperation("获取通道列表")
    public PageResp<Channel> channelList(PageReq pageReq) {
        Channel channel = new Channel();
        channel.setCash(StatusDict.NORMAL);
        PageResp<Channel> pageResp = channelService.findAll(channel, pageReq);
        return pageResp;
    }

    @GetMapping("/businessGroupList/{id}")
    @ApiOperation("获取商户组列表")
    public PageResp<BusinessGroup> businessGroupList(@PathVariable Integer id, PageReq pageReq) {
        BusinessGroup businessGroup = new BusinessGroup();
        businessGroup.setChannelId(id);
        PageResp<BusinessGroup> pageResp = businessGroupService.findAll(businessGroup, pageReq);
        return pageResp;
    }

    @GetMapping("/businessList/{id}")
    @ApiOperation("获取商户列表")
    public PageResp<Map> businessList(@PathVariable Integer id, PageReq pageReq) {
        Business business = new Business();
        business.setGroupId(id);
        PageResp<Business> pageResp = businessService.findAll(business, pageReq);

        // 重新整理返回值
        PageResp<Map> page = new PageResp<>();
        List<Map> list = new ArrayList<>();
        page.setTotalRow(pageResp.getTotalRow());
        page.setTotalPage(pageResp.getTotalPage());
        page.setList(list);
        page.setPageSize(pageResp.getPageSize());
        page.setPageNumber(pageResp.getPageNumber());
        // 查询余额
        for (Business business1 : pageResp.getList()) {
            String accountBalance = withdrawService.queryBusinessBalance(business1.getId());
            Map<String, Object> result = new HashMap<>();
            result.put("account", business1.getAccount());
            result.put("accountBalance", accountBalance);
            result.put("id", business1.getId());
            list.add(result);
        }
        return page;
    }

    @PostMapping
    @ApiOperation("提现")
    public void withdraw(Withdraw withdraw) throws Exception {
        withdrawService.withdraw(withdraw);
    }

    @ApiIgnore
    @RequestMapping("/xjkjNotify")
    public String xjkjwfNotify(HttpServletRequest request) {
        String status = request.getParameter("status");
        String pay_number = request.getParameter("pay_number");
        String orderId = request.getParameter("businessNumber");

        if ("000000".equals(status)) {
            Map<String, String> map = new LinkedHashMap<>();
            map.put("pay_number", pay_number);
            map.put("businessNumber", orderId);
            map.put("status", status);
            Withdraw withdraw = withdrawService.findByOrderNo(pay_number);
            Business business = businessService.findById(withdraw.getBusinessId());
            String sign = DigestUtils.md5Hex(SignUtils.params(map) + business.getSecretKey()).toLowerCase();
            if (sign.equals(request.getParameter("sign"))) {
                withdrawService.success(withdraw.getId());
                return "SUCCESS";
            }
        }
        return "false";
    }
}
